package com.victor9ac.tfm.twitter.client;

import java.util.ArrayList;
import java.util.List;




import java.util.ResourceBundle;

import twitter4j.*;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterClient {

	
	
	/**
	 * my Twitter credentials
	 */
	private String consumerSecret; 
	private String consumerKey;
	private String accessToken;
	private String accessTokenSecret;

	
	ResourceBundle rb = ResourceBundle.getBundle("auth.twitter");
	

	
	/**
	 * Collaborators
	 */
	Twitter twitter;

	public TwitterClient() {
		
		
		consumerKey = rb.getString("consumerKey");
		consumerSecret = rb.getString("consumerSecret");
		accessToken = rb.getString("accessToken");
		accessTokenSecret = rb.getString("accessTokenSecret");
		
		
		twitter = buildTwitterInstance();

	}

	private Twitter buildTwitterInstance() {

		ConfigurationBuilder cb = new ConfigurationBuilder();
		cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey)
				.setOAuthConsumerSecret(consumerSecret)
				.setOAuthAccessToken(accessToken)
				.setOAuthAccessTokenSecret(accessTokenSecret);

		TwitterFactory tf = new TwitterFactory(cb.build());
		Twitter twitterInstance = tf.getInstance();

		return twitterInstance;
	}


	public List<Status> getTweets(String tweetSearch) {
		List<Status> tweetList = new ArrayList<Status>();
		try {
			tweetList = buildTweetQuery(tweetSearch);
		} catch (TwitterException e) {
			e.printStackTrace();
		}
		return tweetList;
	}

	private List<Status> buildTweetQuery(String tweetSearch)
			throws TwitterException {
		Query query = new Query(tweetSearch);
		QueryResult result = twitter.search(query);

		List<Status> tweetList = result.getTweets();
		return tweetList;
	}

}
